package com.heima.xxljob.handle;

import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

//任务调度执行的类
@Component
@Slf4j
public class TestHandle {

    /**
     * 被 @XxlJob()修饰的方法，会自动执行定时任务
     * value值必须和任务调度中心中的任务管理里的JobHandler的值保持一致
     */
    //@XxlJob(value = "testJobHandle")
    public void test(){
        log.info("定时器开始了...."+new Date());
    }

    @XxlJob(value = "testJobHandle")
    public void test1(){
        //分片的参数
        int shardIndex = XxlJobHelper.getShardIndex();//分片索引，0或者1
        int shardTotal = XxlJobHelper.getShardTotal();//2

        //业务逻辑
        List<Integer> list = getList();
        for (Integer integer : list) {
            if(integer % shardTotal == shardIndex){
                System.out.println("当前第"+shardIndex+"分片执行了，任务项为："+integer);
            }
        }
    }
    public List<Integer> getList(){
        List<Integer> list = new ArrayList<>();
        for (int i = 0; i < 10000; i++) {
            list.add(i);
        }
        return list;
    }
}
